 package com.wz.app.activity;

import com.wz.app.R;
import com.wz.app.R.drawable;
import com.wz.app.R.id;
import com.wz.app.R.layout;
import com.wz.app.adapter.FragmentAdapter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements
		OnPageChangeListener, OnClickListener {

	private ViewPager viewPager;
	private TextView newsText, weatherText, settingText;
	private ImageView newsImg, weatherImg, settingImg;
	private LinearLayout newsLay,weatherLay,settingLay;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub

		viewPager = (ViewPager) findViewById(R.id.main_viewPager);
		viewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager()));
		viewPager.setOnPageChangeListener(this);

		newsText = (TextView) findViewById(R.id.news_word);
		newsImg = (ImageView) findViewById(R.id.news_icon);
		weatherText = (TextView) findViewById(R.id.weather_word);
		weatherImg = (ImageView) findViewById(R.id.weather_icon);
		settingText = (TextView) findViewById(R.id.setting_word);
		settingImg = (ImageView) findViewById(R.id.setting_icon);
		newsLay = (LinearLayout) findViewById(R.id.news_layout_button);
		weatherLay = (LinearLayout) findViewById(R.id.weather_layout_button);
		settingLay = (LinearLayout) findViewById(R.id.setting_layout_button);
		
		newsLay.setOnClickListener(this);
		weatherLay.setOnClickListener(this);
		settingLay.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		resetButton();
		switch (v.getId()) {
		case R.id.news_layout_button:
		//	Toast.makeText(this, "这是新闻按钮", Toast.LENGTH_SHORT).show();
			setButtonById(R.id.news_layout_button);
			viewPager.setCurrentItem(0);
			break;
		case R.id.weather_layout_button:
		//	Toast.makeText(this, "这是天气预报按钮", Toast.LENGTH_SHORT).show();
			setButtonById(R.id.weather_layout_button);
			viewPager.setCurrentItem(1);
			break;
		case R.id.setting_layout_button:
		//	Toast.makeText(this, "这是设置按钮", Toast.LENGTH_SHORT).show();
			setButtonById(R.id.setting_layout_button);
			viewPager.setCurrentItem(2);
			break;

		default:
			break;
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// 1:新闻页面  2:天气页面  3:设置页面
		//重置所button 颜色
		resetButton();
		switch (arg0) {
		case 0:
			setButtonById(R.id.news_layout_button);
			break;
		case 1:
			setButtonById(R.id.weather_layout_button);
			break;
		case 2:
			setButtonById(R.id.setting_layout_button);
			break;

		default:
			break;
		}
	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub

	}

	/*
	 * 重置所有button的颜色
	 */
	private void resetButton(){
		newsImg.setImageResource(R.drawable.news_off);
		weatherImg.setImageResource(R.drawable.weather_off);
		settingImg.setImageResource(R.drawable.setting_off);
	}
	private void setButtonById(int id){
		switch (id) {
		case R.id.news_layout_button:
			newsImg.setImageResource(R.drawable.news_on);
			break;
		case R.id.weather_layout_button:
			weatherImg.setImageResource(R.drawable.weather_on);
			break;
		case R.id.setting_layout_button:
			settingImg.setImageResource(R.drawable.setting_on);                                           
			break;

		default:
			break;
		}
	}
	
	
	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// getMenuInflater().inflate(R.menu.main, menu);
	// return true;
	// }
	//
	// @Override
	// public boolean onOptionsItemSelected(MenuItem item) {
	// // Handle action bar item clicks here. The action bar will
	// // automatically handle clicks on the Home/Up button, so long
	// // as you specify a parent activity in AndroidManifest.xml.
	// int id = item.getItemId();
	// if (id == R.id.action_settings) {
	// return true;
	// }
	// return super.onOptionsItemSelected(item);
	// }
}
